<template>
    <div>
        <div class="page-action-btn" style="justify-content: space-between;color:#7F7F7F;font-weight: bold;">
            <div>权限组</div>
            <p-confirm-btn type="info" :show-icon="false" @confirm="getAppCode" v-if="UIAuth._api_admin_auth_group_app_code_id_get && tableRow" msg="确定要获取【权限标识】吗？">获取权限标识</p-confirm-btn>
        </div>
        <div v-if="UIAuth._api_admin_auth_group_get">
            <p-table v-loading="loading" :data="dataList" :show-header="false" :sub-height="subHeight" :check-box="!select" :show-index="!select" :show-paginate="false" :select-index="selectIndex"
                @row-click="rowClick" @row-clear="rowClear">
                <el-table-column prop="title" :label="select?'权限组':'名称'"></el-table-column>
            </p-table>
        </div>
    </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs } from "vue";
import { useStore } from "vuex";
import PTable from "@/components/PTable.vue";
import PElDialog from "@/components/PElDialog.vue";
import http from "@/lib/http/http";
import PConfirmBtn from "@/components/PConfirmBtn.vue";
import message from "@/lib/message";
/**
 * state 数据类型
 */
interface IState {
    loading: boolean;
    dataList: any[];
    tableRow: any;
    shareId: string;
}
export default defineComponent({
    name: "AuthGroupList",
    components: { PTable, PElDialog, PConfirmBtn },
    props: {
        subHeight: {
            type: Number,
            default: 0,
        },
        select: {
            type: Boolean,
            default: false,
        },
        showShare: {
            type: Boolean,
            default: true,
        },
        // 是否默认选择第 N行
        selectIndex: {
            type: Number,
            default: 1,
        },
    },
    emits: ["change", "data-list"],
    setup(props, { emit }) {
        const store = useStore();
        const { UIAuth, bodySize } = toRefs(store.state);
        const state = reactive<IState>({
            loading: false,
            dataList: [],
            tableRow: null,
            shareId: "-1",
        });
        /**
         * 单击选中
         */
        const rowClick = (val: any) => {
            state.tableRow = val.row;
            if (props.select) {
                emit("change", val.row);
            }
        };
        /**
         * 取消选中
         */
        const rowClear = () => {
            state.tableRow = null;
            if (props.select) {
                emit("change", null);
            }
        };
        /**
         * 获取内容
         */
        const getList = () => {
            state.loading = true;
            http.Get(`/auth-group/share-id`)
                .then((data) => {
                    state.shareId = data;
                    http.Get(`/auth-group`)
                        .then((dataList) => {
                            let list: any[] = [];
                            if (props.showShare) {
                                list = dataList;
                            } else {
                                dataList.forEach((item: any) => {
                                    if (item.id !== state.shareId) {
                                        list.push(item);
                                    }
                                });
                            }
                            state.dataList = list;
                            emit("data-list", list);
                            state.loading = false;
                        })
                        .catch((err) => (state.loading = false));
                })
                .catch((err) => false);
        };
        /**
         * 获取权限标识
         */
        const getAppCode = () => {
            if (state.tableRow == null) {
                message.error("请选择要获取的权限组");
                return;
            }
            message.loadingShow("获取中，请稍后");
            http.Get(`/auth-group/app-code/${state.tableRow.id}`)
                .then((data) => {
                    message.alert(data, {
                        title: "您的【权限标识 ( AppCode ) 】为：",
                        showIcon: false,
                    });
                })
                .catch((err) => false);
        };
        // 初始化
        getList();

        return {
            UIAuth,
            bodySize,
            ...toRefs(state),
            rowClick,
            rowClear,
            getAppCode,
        };
    },
});
</script>

<style scoped lang="scss">
</style>